/**
 * Test SDL 1.3 speed
 */
#include "SDL.h"
#include <time.h>
#include <math.h>
#include <stdlib.h>

#define WINDOW_WIDTH (1024)
#define WINDOW_HEIGHT (768)
#define BALL_IMG "life.bmp"

SDL_WindowID win;
SDL_TextureID ball;

static
void
quit (int rc) {
 exit(rc);
}

static
void init () {
 /*if (SDL_VideoInit(NULL,0) < 0) {
  fprintf(stderr,"Couldn't initialize SDL Video.\n");
  fprintf(stderr,"SDL said: %s\n", SDL_GetError());
  quit(EXIT_FAILURE);
 }*/

 //SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

 win = SDL_CreateWindow("SDL 1.3 bounce test",
                         SDL_WINDOWPOS_UNDEFINED,
                         SDL_WINDOWPOS_UNDEFINED,
                         WINDOW_WIDTH, WINDOW_HEIGHT,
//                         SDL_WINDOW_OPENGL|
                        // SDL_WINDOW_FULLSCREEN|
                         SDL_WINDOW_SHOWN);
 if (!win) {
   quit(EXIT_FAILURE);
 }
 // VSYNC slows down much
/* if (SDL_CreateRenderer(win, 0, SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTFLIP2)) {
  fprintf(stderr,"Couldn't create a renderer.\n");
  fprintf(stderr,"SDL said: %s\n", SDL_GetError());
  quit(EXIT_FAILURE);
 }
 SDL_SelectRenderer(win);*/

 srandom(time(0));
}

static
void loadResources () {
 SDL_Surface* ball_surf = SDL_LoadBMP(BALL_IMG);
 if (ball_surf == NULL) {
  fprintf(stderr,"Couldn't load image file: %s\n", BALL_IMG);
  quit(EXIT_FAILURE);
 }

 

 /*SDL_RendererInfo rinfo;
 SDL_GetRendererInfo(&rinfo);

 printf("*** INFO: Renderer name: %s\n", rinfo.name);*/
 ball = SDL_CreateTextureFromSurface(0, ball_surf);

  if (!ball) {
  fprintf(stderr,"Unable to create texture from image file: %s\n", BALL_IMG);
  fprintf(stderr,"SDL said: %s\n", SDL_GetError());
  SDL_FreeSurface(ball_surf);
  quit(EXIT_FAILURE);
 }
 // Not needed
 SDL_FreeSurface(ball_surf);
}

static
void freeResources () {
 SDL_DestroyRenderer(win);
}

static
void bounce () {
  int w;
  int h;
  SDL_QueryTexture(ball,NULL,NULL,&w,&h);
  SDL_Rect position = {
   0.5 * (WINDOW_WIDTH - w),
   0.5 * (WINDOW_HEIGHT - h),
   w,
   h
  };
  float x = position.x;
  float y = position.y;
  float angle = 0.f;
  float dx = 1.f;
  float dy = 1.f;

  SDL_SetRenderDrawColor(0x00,0x00,0x00,0xFF);
  SDL_RenderRect(NULL);

  SDL_Event evt;

  /* Generate a random initial direction */
  angle = 2.f * (double)M_PI * ((double)random() / (double)RAND_MAX);
  //printf ("Rand = %.9f\n", (double)random()/((double)RAND_MAX+(double)1));
  dx = cos(angle);
  dy = sin(angle);

  int done = 0;

  printf ("dx = %.4f, dy = %.4f\n", dx, dy);
  printf ("norm = %.4f\n", dx*dx+dy*dy);
  do {
    //SDL_RenderRect(NULL);
    SDL_RenderCopy(ball,NULL,&position);
    SDL_RenderPresent();
    //SDL_GL_SwapWindow(win);
    //SDL_Delay(1);

    /* Randomly change the direction
       to avoid linearity.
       Raise probability to have a nice 'painting' effect */
    /*if ((rand() / (float)RAND_MAX) > 0.94f) { 
      dx += .01 * (float)rand() / (float)RAND_MAX;
    }
    if ((rand() / (float)RAND_MAX) > 0.94f) { 
      dy += .01 * (float)rand() / (float)RAND_MAX;
    }*/
    /*if ((rand() / (float)RAND_MAX) > 0.94f) { 
     if (rand() / (float)RAND_MAX > 0.5f) {
       angle += 0.01f * (float)rand() / (float)RAND_MAX;
     }
     else {
       angle -= 0.01f * (float)rand() / (float)RAND_MAX;
     }
     dx = cos(angle);
     dy = sin(angle);
    }*/

    if ((x+w+dx >= WINDOW_WIDTH) || (x+dx < 0)) {
     dx = -dx;
    }
    if ((y+h+dy >= WINDOW_HEIGHT) || (y+dy < 0)) {
     dy = -dy;
    }

    SDL_Rect clear = {
     position.x - 1,
     position.y - 1,
     position.w + 2,
     position.h + 2
    };
    //SDL_RenderRect(&clear);
    
    x += dx;
    y += dy;
    position.x = (int)x;
    position.y = (int)y;
  

    while (SDL_PollEvent(&evt)) {
     if (evt.type == SDL_QUIT) {
      done = 1;
      break;
     }
     if (evt.type == SDL_KEYDOWN) {
       done = 1;
       break;
     }
    }
  }while(!done);
}


int main (int argc, char **argv) {
 init();
 loadResources();
 bounce();
 freeResources();
 quit(EXIT_SUCCESS);
 return 0;
}

